package net.wesley.android.city;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class ChengDu extends City {

	private String chejia6;
	protected String verifycode="";

	public void requestParamsFinish(String[] values) {
		this.chejia6=values[0];
		dostep1();
	}

	private void dostep1() {
		//new HttpConnection(ctx,step1handler,"GBK").get("http://www.cdjg.gov.cn/");
		DefaultHttpClient httpClient = new DefaultHttpClient();  
		try {
			httpClient.execute(new HttpGet("http://www.cdjg.gov.cn/"));
			httpClient.execute(new HttpGet("http://www.cdjg.gov.cn/codetest.asp"));
			List<Cookie> cookies = httpClient.getCookieStore().getCookies();
			for (int i=0;i<cookies.size();i++){
				if (cookies.get(i).getName().equals("verifycode")){
					verifycode=cookies.get(i).getValue();
				}
			}
			if (verifycode.length()>0){
		        String url="http://www.cdjg.gov.cn/view11.asp?cllx=02&car_number=%B4%A8"+chepai.substring(1)+"&car_cj="+chejia6+"&vcode="+this.verifycode+"&Submit=%B2%E9%D1%AF";
				HttpGet get = new HttpGet(url);
				get.setHeader("Referer","http://www.cdjg.gov.cn/");
		        HttpResponse rsp = httpClient.execute(get);
				BufferedReader br;
				br = new BufferedReader(new InputStreamReader(rsp.getEntity()
						.getContent(),"GBK"));
				String line, result = "";
				while ((line = br.readLine()) != null) result += line+"\n";
				processResult(result);
			}
		} catch (ClientProtocolException e) {
			callback.onComplete(-10001, "");
			e.printStackTrace();
		} catch (IOException e) {
			callback.onComplete(-10002, "");
			e.printStackTrace();
		}
	}

	private void processResult(String result) {
		int idx=result.indexOf("<font size=2 color=#ff0000>");
		if (idx>0){
			int idx2=result.substring(idx+27).indexOf("</font></b><br><br><br><br>");
			if (idx2>0){
				callback.onComplete(-10003, "ret:"+result.substring(idx+27).substring(0,idx2));
				return;
			}
		}
		
		idx=result.indexOf("违法信息:( 总共有 ");
		if (idx>0){
			int idx2=result.substring(idx+11).indexOf(" 条记录)");
			if (idx2>0){
				int total=Integer.parseInt(result.substring(idx+11).substring(0,idx2));
				String str=result.substring(idx+11+idx2);
				str=str.split("接受处罚地点")[1];
				str=str.split("</TABLE>")[0];
				String[] rows=str.split("<TR");
				boolean firstrow=true;
				String retvalue="";
				for (int i=0;i<rows.length;i++){
					if (firstrow){
						firstrow=false;
						continue;
					}
					String row=rows[i];
					String[] cols = row.split("<td");
					String shijian = cols[1].split(">")[1].split("<")[0];
					String didian = cols[4].split(">")[1].split("<")[0];
					String code = cols[3].split(">")[1].split("<")[0];
					retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",shijian,didian,code);
				}
				callback.onComplete(total, retvalue);
				return;
			}
		}
		callback.onComplete(0, "");
		return;
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("川A");
	}

	@Override
	public String[] paramDesc() {
		String[] s={"车架号后六位"};
		return s;
	}

}
